package zyc.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @description:
 * @author: Code-zyc
 * @date: Created in 2020/12/21 9:59
 * @version: v1.0
 * @modified By:
 */
public class JdbcUtils {


    //获取数据库的链接
    private static DruidDataSource source;
    static {
        try {
            InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties ps=new Properties();
            ps.load(is);
            source = (DruidDataSource) DruidDataSourceFactory.createDataSource(ps);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   // :获取数据库链接



    public static Connection getconnection()  {

        Connection connection = null;
        try {
            connection = source.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }




 //关闭数据库链接


    public static void closeconnection(Connection connection){
        if (connection!=null){

            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
